//
//  MKVideoViewController.m
//  ManKeCartoon
//
//  Created by dllo on 15/10/24.
//  Copyright © 2015年 ManKe. All rights reserved.
//

#import "MKVideoViewController.h"
#import "MKVideoBFViewController.h"
#import "MKBaseHeader.h"

@interface MKVideoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@end

@implementation MKVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

//    self.view.backgroundColor = [UIColor redColor];
    
    
     self.tabBarController.tabBar.hidden = YES;
    
    
    
    _titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [_titleView setBackgroundColor:[UIColor colorWithRed:0.91 green:0.42 blue:0.24 alpha:1]];
    [self.view addSubview:_titleView];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/2-50, 24, 100, 30)];
    
    
    [titleLabel setText:@"动画专区"];
    [titleLabel setTextAlignment:NSTextAlignmentCenter];
    titleLabel.textColor = [UIColor whiteColor];

    [self.titleView addSubview:titleLabel];
    
    [self videoColloction];

    [self setUpBackButton];
    [self AFNdata];
}

#pragma mark - 创建一个返回按钮
- (void)setUpBackButton
{
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setFrame:CGRectMake(10, 18, 30, 30)];
    [_backButton setImage:[UIImage imageNamed:@"iconfont-arrowleft.png"] forState:UIControlStateNormal];
    [_backButton addTarget:self action:@selector(backButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [self.titleView addSubview:_backButton];
}
#pragma mark - 返回按钮点击方法
- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)AFNdata
{
    
        
        [MKAFNmodel getdataWithString:@"http://mobilev3.ac.qq.com/Cartoon/cartoonList/" block:^(id block) {
            
            _VideoArray = [[NSMutableArray alloc]init];
            
            for (NSMutableDictionary *dic in [block objectForKey:@"data"]) {
                
                MKChartsModel *model = [[MKChartsModel alloc]init];
                
                [model setValuesForKeysWithDictionary:dic];
                
                [_VideoArray addObject:model];
                
                
            }
        
            [self.videoCollectionView reloadData];
        
        }];
    
}
-(void)videoColloction
{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, 160*HEIGHT);
    
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;
    
    
    _videoCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height-40) collectionViewLayout:flowLayout];
    _videoCollectionView.delegate =self;
    _videoCollectionView.dataSource = self;
    _videoCollectionView.backgroundColor = [UIColor whiteColor];
    
    [_videoCollectionView registerClass:[MKVideoCollectionViewCell class] forCellWithReuseIdentifier:@"MKVideoCollectionViewCell"];
    [self.view addSubview:_videoCollectionView];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section;
{
    return _VideoArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    MKVideoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"MKVideoCollectionViewCell" forIndexPath:indexPath];
    
    MKChartsModel *model = [[MKChartsModel alloc]init];
    model = _VideoArray[indexPath.row];
    
    cell.title.text = model.title;
    
    cell.lastup.text = model.lastup;
   
    NSURL *url = [NSURL URLWithString:model.cover_url];
    [cell.cover_url sd_setImageWithURL:url];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MKVideoBFViewController *videoVc = [[MKVideoBFViewController alloc] init];
    
    MKChartsModel *model = [[MKChartsModel alloc] init];
    model = _VideoArray[indexPath.row];
    videoVc.dhName = model.title;
    videoVc.comic_id = model.comic_id;
    [self.navigationController pushViewController:videoVc animated:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
